package com.blockmeta.bbs.baselibrary.utils;

import android.text.TextUtils;

import com.blankj.utilcode.util.StringUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;

/**
 * Created by author on 2018/1/10.
 */

public class TimeUtil {
    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    /**
     * 返回文字描述的日期
     *
     * @param time
     * @return
     */
    public static String getTimeFormatText(int time) {
        long newTime = time * 1000L;
        Date date = new Date(newTime);
        long diff = System.currentTimeMillis() - date.getTime();
        long r = 0;
        if (diff > year) {
            return FormatUtil.getTimetoHMS(newTime, "yyyy-MM-dd");
        }
        if (diff > month) {
            return FormatUtil.getTimetoHMS(newTime, "yyyy-MM-dd");
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    /**
     * 返回文字描述的日期
     *
     * @param time
     * @return
     */
    private static DecimalFormat mDecimalFormat = new DecimalFormat("0.#");
    public static String getUserRegisterAge(String registerAge) {
        if(TextUtils.isEmpty(registerAge)){
            return "未知";
        }
        if(registerAge.length() < 10){
            return registerAge;
        }
        long newTime = 0L;
        try {
            newTime = Integer.parseInt(registerAge) * 1000L;
        }catch (NumberFormatException e){
            return "未知";
        }

        Date date = new Date(newTime);
        long diff = System.currentTimeMillis() - date.getTime();
        long r;
        if (diff > year) {
            double y = CalculateUtil.div(diff,year,1);
            return mDecimalFormat.format(y) + " 年";
        }
        if (diff > month) {
            r = (diff / month);
            if (diff%month!=0){
                r += 1;
            }
            return r>=12?"1 年":r + " 个月";
        }
        else  {
            return "1 个月";
        }
    }


    public static String InforMationTime(int time) {
        long newTime = time * 1000L;
        Date date = new Date(newTime);
        long diff = System.currentTimeMillis() - date.getTime();
        long r = 0;
        Calendar cal = Calendar.getInstance();
        int CurrentYear = cal.get(Calendar.YEAR);


        if (diff > year || CurrentYear != FormatUtil.parseInt(FormatUtil.getTimetoHMS(newTime, "yyyy"))) {
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "yyyy.MM.dd HH:mm");
        }
        if (diff > month) {
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "MM.dd HH:mm");
        }


        //判断是不是昨天
        if (IsYesterday(time)) {
            return "昨天 " + FormatUtil.getTimetoHMS(newTime, "HH:mm");
        }
        if (diff > day) {//大于24小时秒值
//            r = (diff / day);
//            if (r == 1) {
//                return "前天 " + CommenUtil.getTimetoHMS(newTime, "HH:mm");
//            }
//            if (r > 1 && r < 4) {
//                return r + "天前 " + CommenUtil.getTimetoHMS(newTime, "HH:mm");
//            }
//            if (r >= 4) {
                return FormatUtil.getTimetoHMS((int) (newTime / 1000), "MM.dd HH:mm");
//            }
        }
        //判断是不是今天
        if (IsToday(time)) {
            if (diff > hour) {
                r = (diff / hour);
                return r + "小时前";

            }
            if (diff > minute) {
                r = (diff / minute);
                return r + "分钟前";
            }
        }

        return "刚刚";
    }


    /**
     * 判断是否为今天(效率比较高)
     *
     * @param seconds 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(int seconds) {
        String day = FormatUtil.getTimetoHMS(seconds);
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = getDateFormat().parse(day);
        } catch (ParseException e) {

        }
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                                  - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param seconds 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsYesterday(int seconds)  {
        String day = FormatUtil.getTimetoHMS(seconds);
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = getDateFormat().parse(day);
        } catch (ParseException e) {
        }
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                                  - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    


    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();



    /**
     * 取得1970.1.1至今的秒數
     *
     * @return
     */
    public static long getSeconds() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 取得Offset的日期
     *
     * @param date
     * @param offset offset的日數(+/-)
     * @return
     */
    public static int getDateOffset(int date, int offset) {
        Calendar cal = Calendar.getInstance();
        cal.set(getYear(date), getMonth(date) - 1, getDay(date));
        cal.add(Calendar.DATE, offset);

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);

        return year * 10000 + month * 100 + day;
    }

    /**
     * 取得年份
     *
     * @param date YYYYMMSS整數值
     * @return YYYY
     */
    public static int getYear(int date) {
        return date / 10000;
    }

    /**
     * 取得月份
     *
     * @param date YYYYMMSS整數值
     * @return MM
     */
    public static int getMonth(int date) {
        return (date % 10000) / 100;
    }

    /**
     * 取得一年中的第幾周
     *
     * @param date YYYYMMSS整數值
     * @return Week of Month
     */
    public static int getWeekofYear(int date) {
        Calendar cal = Calendar.getInstance();
        cal.set(getYear(date), getMonth(date) - 1, getDay(date));
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 取得星期幾
     *
     * @param date YYYYMMSS整數值
     * @return Day of Week
     */
    public static int getDayofWeek(int date) {
        Calendar cal = Calendar.getInstance();
        cal.set(getYear(date), getMonth(date) - 1, getDay(date));
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 判断是否是周末
     *
     * @param calendar
     * @return
     */
    public static boolean isWeekend(Calendar calendar) {
        int intNow = calendar.get(Calendar.HOUR_OF_DAY) * 100
                + calendar.get(Calendar.MINUTE);

        boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);

        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0) {
                weekDay = 7;
            }
        }

        return weekDay == 6 || weekDay == 7;
    }

    /**
     * 取得日
     *
     * @param date YYYYMMSS整數值
     * @return SS
     */
    public static int getDay(int date) {
        return date % 100;
    }

    public static int getHour(int time) {
        return time / 10000;
    }

    public static int getShortHour(int time) {
        return time / 100;
    }

    public static int getMinute(int time) {
        return (time % 10000) / 100;
    }

    public static int getShortMinute(int time) {
        return time % 100;
    }

    public static int getSecond(int time) {
        return time % 100;
    }


    /**
     * Parse Second start from 00:00:00
     *
     * @param time 2:00:00 (7:00:00)
     * @return
     */
    public static int getSecond(String time) {
        int second = 0;

        if (!TextUtils.isEmpty(time)) {
            try {
                StringTokenizer tokenizer = new StringTokenizer(time, ":");
                while (tokenizer.hasMoreTokens()) {
                    String item = tokenizer.nextToken();
                    second = second * 60 + Integer.parseInt(item);
                }
            } catch (Exception e) {
            }
        }

        return second;
    }

    /**
     * @param n       -1 表示前一周 0 表示本周 1表示下一周
     * @param weekDay 想要的某一周的周几
     * @return 某一周的周几对应的时间戳
     */
    public static long getWeekTime(int n, int weekDay, boolean isDayStart) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.add(Calendar.DATE, n * 7);
        calendar.set(Calendar.DAY_OF_WEEK, weekDay);
        if (isDayStart) {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
        }
        return calendar.getTimeInMillis();
    }
}